<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.mobile"
          clearable
          size="small"
          placeholder="用户账号"
          style="width: 180px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.parentTel"
          clearable
          size="small"
          placeholder="邀请人账号"
          style="width: 180px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- <el-input
          v-model="query.inviteCode"
          clearable
          size="small"
          placeholder="用户邀请码筛选"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <!-- <el-input
          v-model="query.uid"
          clearable
          size="small"
          placeholder="用户ID筛选"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <el-select
          v-model="query.type"
          clearable
          size="small"
          style="width: 180px"
          class="filter-item"
          placeholder="用户类型"
        >
          <el-option
            v-for="(key, value) in {
              1: '普通用户',
              2: '有效用户',
            }"
            :key="key"
            :label="key"
            :value="value"
          />
        </el-select>

        <el-select
          v-model="query.level"
          clearable
          size="small"
          style="width: 180px"
          class="filter-item"
          placeholder="用户等级"
        >
          <el-option
            v-for="(key, value) in {
              0: '暂无',
              1: 'V1',
              2: 'V2',
              3: 'V3',
            }"
            :key="key"
            :label="key"
            :value="value"
          />
        </el-select>
        <el-select
          v-model="query.node"
          clearable
          size="small"
          style="width: 180px"
          class="filter-item"
          placeholder="节点等级"
        >
          <el-option
            v-for="(key, value) in {
              0: '暂无',
              1: '社区节点',
              2: '超级节点',
              3: '股东节点',
            }"
            :key="key"
            :label="key"
            :value="value"
          />
        </el-select>
        <!-- <el-date-picker
          v-model="params.date"
          clearable
          type="datetimerange"
          size="small"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="filter-item"
          range-separator="~"
          style="width: 380px"
          start-placeholder="请选择开始日期"
          end-placeholder="请选择结束日期"
          :picker-options="pickerDisabled"
        /> -->

        <!-- <el-button
          v-permission="permission.export"
          type="primary"
          class="filter-item"
          size="mini"
          @click="$duduxia({...params},'/admin/bok/user/outList',$route.meta.title)"
        >数据导出</el-button
        >    -->
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission" />
      <!-- <el-button type="primary" @click="downloadFile">数据导出</el-button> -->
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      stripe
      :row-key="getRowKeys"
      @selection-change="selectionChange"
    >
      <!-- <el-table-column type="selection" width="55" :reserve-selection="true" /> -->
      <!-- <el-table-column
        v-if="columns.visible('userId')"
        :show-overflow-tooltip="true"
        prop="userId"
        label="UID"
      /> -->
      <el-table-column
        v-if="columns.visible('mobile')"
        :show-overflow-tooltip="true"
        prop="mobile"
        label="账号"
      />
      <el-table-column
        v-if="columns.visible('nick')"
        :show-overflow-tooltip="true"
        prop="nick"
        label="用户昵称"
      />
      <el-table-column
        v-if="columns.visible('parentTel')"
        :show-overflow-tooltip="true"
        prop="parentTel"
        label="邀请人账号"
      />
      <el-table-column
        v-if="columns.visible('type')"
        :show-overflow-tooltip="true"
        prop="type"
        label="用户类型"
      >
        <template slot-scope="scope">{{
          { 0: "有效", 1: "无效" }[scope.row.type]
        }}</template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('level')"
        :show-overflow-tooltip="true"
        prop="level"
        label="用户等级"
      >
        <template slot-scope="scope">{{
          { 0: "暂无", 1: "V1", 2: "V2", 3: "V3" }[scope.row.level]
        }}</template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('nodeLevel')"
        :show-overflow-tooltip="true"
        prop="nodeLevel"
        label="节点等级"
      >
        <template slot-scope="scope">{{
          { 0: "暂无", 1: "社区节点", 2: "超级节点", 3: "股东节点" }[
            scope.row.nodeLevel
          ]
        }}</template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('status')"
        :show-overflow-tooltip="true"
        prop="status"
        label="状态"
      >
        <template slot-scope="scope">{{
          { 0: "正常", 1: "黑名单"}[
            scope.row.status
          ]
        }}</template>
      </el-table-column>
      <!-- <el-table-column
        v-if="columns.visible('createAt')"
        :show-overflow-tooltip="true"
        prop="createAt"
        label="注册时间"
      /> -->
      <!-- <el-table-column
        v-if="columns.visible('bokNode')"
        :show-overflow-tooltip="true"
        prop="bokNode"
        label="注册时间"
      />
      <el-table-column
        v-if="columns.visible('usdtCharge')"
        :show-overflow-tooltip="true"
        prop="usdtCharge"
        label="USDT累计充值"
      />
      <el-table-column
        v-if="columns.visible('usdtWithdrawal')"
        :show-overflow-tooltip="true"
        prop="usdtWithdrawal"
        label="USDT累计提现"
      />
      <el-table-column
        v-if="columns.visible('delFlag')"
        prop="delFlag"
        label="是否有效"
      >
        <template slot-scope="scope">{{
          { 0: "有效", 1: "无效" }[scope.row.delFlag]
        }}</template>
      </el-table-column> -->
      <el-table-column
        v-if="columns.visible('createAt')"
        prop="createAt"
        :show-overflow-tooltip="true"
        label="注册时间"
      >
        <template slot-scope="scope">
          <div>
            {{ $moment(scope.row.createAt).format("YYYY-MM-DD HH:mm:ss") }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220px" prop="id" fixed="right">
        <template slot-scope="scope">
          <!-- v-permission="permission.view" -->
          <el-button
            size="mini"
            type="text"
            @click="openDialog(scope.row)"
          >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="pullDark(scope.row.id)"
          >加入黑名单</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="goDetail(scope.row.id)"
          >查看详情</el-button
          >
          <!-- <el-button
            v-permission="permission.recharge"
            size="mini"
            type="text"
            @click="openDialog(scope.row)"
          >充值</el-button
          >-->
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />

    <!-- 充值弹窗 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="payform"
        :model="payform"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="数量" prop="amount">
          <el-input v-model="payform.amount" style="width: 75%" />
        </el-form-item> -->
        <el-form-item label="会员等级" prop="level">
          <el-select v-model="payform.level" placeholder="请选择币种">
            <el-option label="暂无" :value="0" />
            <el-option label="V1" :value="1" />
            <el-option label="V2" :value="2" />
            <el-option label="V3" :value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label="节点等级" prop="nodeLevel">
          <el-select v-model="payform.nodeLevel" placeholder="请选择币种">
            <el-option label="暂无" :value="0" />
            <el-option label="社区节点" :value="1" />
            <el-option label="超级节点" :value="2" />
            <el-option label="股东节点" :value="3" />

          </el-select>
        </el-form-item>

        <!-- <el-form-item label="备注">
          <el-input type="textarea" v-model="payform.remake"></el-input>
        </el-form-item>-->
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit('payform')">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import customerCrudMethods from '@/api/customer'
import { userCharge } from '@/api/customer'
import CRUD, { presenter, header, crud, form } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { downloadFile } from '../../utils'
import { exportCustomerExcel } from '../../api/exportApi'
// crud交由presenter持有
const defaultCrud = CRUD({
  url: '/admin/user/page',
  title: '用户',
  crudMethod: { ...customerCrudMethods }
})
const defaultForm = {
  delFlag: null,
  uid: null,
  inviteCode: null,
  param: 0
}
export default {
  name: 'Customer',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      params: {},
      permission: {
        view: ['admin', 'customer:view'],
        recharge: ['admin', 'customer:recharge'],
        export: ['admin', 'customer:export'],
        add: ['admin'],
        edit: ['admin'],
        del: ['admin']
      },
      dialogVisible: false,
      giftList: [],
      payform: {
        userId: null,
        coinId1: null,
        amount: null
      },
      rules: {
        amount: [
          { required: true, message: '请输入充值数量', trigger: 'blur' },
          {
            pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/,
            message: '请输入正数',
            trigger: 'blur'
          }
        ],
        coinId1: [{ required: true, message: '请选择币种', trigger: 'change' }],
        assetType: [
          { required: true, message: '请选择账户类型', trigger: 'change' }
        ]
      },
      check: true,
      pickerDisabled: {
        disabledDate: (time) => {
          return time.getTime() > new Date().getTime()
        }
      }
    }
  },
  created() {},
  methods: {
    [CRUD.HOOK.beforeReset]() {
      this.params = {}
    },
    [CRUD.HOOK.beforeRefresh](crud) {
      const { date } = this.params
      if (date) {
        const [startDate, endDate] = date
        crud.query.startDate = startDate
        crud.query.endDate = endDate
      } else {
        crud.query.startDate = ''
        crud.query.endDate = ''
      }
    },
    [CRUD.HOOK.beforeToEdit](crud) {
      crud.form.parentAccount = crud.form.parentPhone
    },
    handleClose(done) {
      this.dialogVisible = false
    },
    openDialog(data) {
      this.dialogVisible = true
      this.payform.userId = data.id
      this.payform.nodeLevel = data.nodeLevel
      this.payform.level = data.level
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            ...this.payform
          }
          // params.coinId1 == 1 ? (params.coinId = 1) : (params.coinId = 2);
          // params.coinId1 == 3 ? (params.assetType = 1) : (params.assetType = 0);
          // console.log(params)
          //   return;
          userCharge({ ...params, delFlag: 0 }).then((res) => {
            if (res.code == 0) {
              this.$message.success('编辑成功')
              this.dialogVisible = false
              this.crud.toQuery()
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    pullDark() { // 拉黑
      userCharge({ ...params, delFlag: 1 }).then((res) => {
        if (res.code == 0) {
          this.$message.success('成功加入黑名单')
          this.dialogVisible = false
          this.crud.toQuery()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    goDetail(id) {
      this.$router.push({
        path: '/customer/detail',
        query: { userId: id }
      })
    },
    getRowKeys(row) {
      return row.uuid // 每条数据的唯一识别值
    },
    selectionChange(val) {
      this.giftList = val
      console.log(this.giftList)
      if (val.length > 0) {
        this.check = false
      } else {
        this.check = true
      }
    },
    downloadFile() {
      exportCustomerExcel({
        ...this.crud.query
      }).then((res) => {
        downloadFile(res.data, '用户', 'xlsx')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.red {
  color: red;
}
</style>
